package com.spacing.app;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;
import com.google.analytics.tracking.android.EasyTracker;
import com.senseilearning.anatomy.R;

public class ChapterGridViewActivity extends SherlockActivity {
	private GridView gridView;
	private ChapterAdapter adapter;
	static final String[] chapterNames = new String[] { "A", "B", "C", "D", "E", "F" };
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chapters_grid);
		gridView = (GridView) findViewById(R.id.gridViewChapter);
		adapter = new ChapterAdapter(this, chapterNames); 
		
		gridView.setAdapter(adapter);
		
		gridView.setDrawSelectorOnTop(true);
		gridView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				
			}
		});
		
	}
	
	//onStart and onStop are now only used for calling the analytics methods
	@Override
	public void onStart(){
		super.onStart();
		//New code for this method must be added before the easytracker line!
		EasyTracker.getInstance().activityStart(this);
	}
	
	@Override
	public void onStop() {
		super.onStop();
		//New code for this method must be added before the easytracker line!
		EasyTracker.getInstance().activityStop(this);
	}
	
	
	public class ChapterAdapter extends BaseAdapter {
    	private Context context;
    	private final String[] chapterNames;
     
    	public ChapterAdapter(Context context, String[] chapterNames) {
    		this.context = context;
    		this.chapterNames = chapterNames;
    	}
     
    	public View getView(int position, View convertView, ViewGroup parent) {
     
    		LayoutInflater inflater = (LayoutInflater) context
    			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     
    		View gridItem;
     
    		if (convertView == null) {

    			
    			gridItem = new View(context);
    			gridItem = inflater.inflate(R.layout.chapter, null);
    			TextView textView = (TextView) gridItem.findViewById(R.id.chapter_name);
    			AspectRatioImageView imageView = (AspectRatioImageView) gridItem.findViewById(R.id.myImageView);
    			String uri = "drawable/chapter"; 
    			int imageResource = getResources().getIdentifier(uri, null, getPackageName());
    		    Drawable image = getResources().getDrawable(imageResource);
    		    
    		    imageView.setImageDrawable(image);
    		    
    			textView.setText(chapterNames[position]);

        		
    		} else {
    			gridItem = convertView;
    		}
    		
    		return gridItem;
    	}

		public int getCount() {
			// TODO Auto-generated method stub
			return chapterNames.length;
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}
     
    }

}